Random = class("Random")

Random.verbose = false

---

function Random:initialize()

	gRandom = self
	
	self.biasedContextes = {}

	math.randomseed( os.time() )
	math.random(); math.random(); math.random()	
end
 
---

function Random:addContextualBias(context, valueToReturn)

	self.biasedContextes[(#self.biasedContextes) + 1] = 
	{
		context = context, 
		valueToReturn = valueToReturn
	}
end

---

function Random:get(context)


	if (Random.verbose) then
	
		print("Random context :")
		debug.printTable(context)
	end
	
	
	-- look for biased random (for unit tests)
	
	for i, contextAndValue in ipairs(self.biasedContextes) do
	
		local foundDifference = false
		
		for i, contextEntry in ipairs(context) do
		
			if contextEntry ~= contextAndValue.context[i] then
				
				foundDifference = true
				break
			end
		end
		
		if not foundDifference then
	
			if (Random.verbose) then
			
				print(" -> return biased random : " .. tostring(contextAndValue.valueToReturn))
			end
				
			return contextAndValue.valueToReturn, true
		end
	end

	
	-- return normal random
	
	local randomValue = math.random()
	
	if (Random.verbose) then
	
		print(" -> return unbiased random : " .. randomValue)
	end
		
	return randomValue, false
end